<?php

namespace App\Console\Commands\model;

use App\Dep\ModelCategorysDep;
use App\Dep\ModelDep;
use App\Dep\SystemDep;
use App\Dep\TextToImageCategorysDep;
use App\Dep\TextToImageDep;
use App\Enum\TextToImageEnum;
use App\Lib\ModelSdk;
use App\Service\UidService;
use Illuminate\Console\Command;
class TextToImageCmd extends Command
{
    //打标
    public $signature = 'model:text-to-image';
    public function handle()
    {
        $dep = new TextToImageDep();
        $resWait = $dep->getWaitImage();
        if($resWait->isEmpty()){
            $this->log('have no wait tag');
            return false;
        }

        $loraDep = new ModelDep();
        $loraCateDep = new ModelCategorysDep();
        $textToImageCateDep = new TextToImageCategorysDep();
        $service = new ModelSdk();
        $systemDep = new SystemDep();

        $resSystem = $systemDep->first();

        $resWait->each(function($item) use ($service,$loraDep,$textToImageCateDep,$dep,$resSystem,$loraCateDep) {
            try{
                $resLora = $loraDep->first($item->lora_id);
                $resCate = $textToImageCateDep->first($item->second_category);
                $resLoraCate = $loraCateDep->first($resLora->second_category);

                $res= $service->textToImage($resLora->name, $item->key, $item->width, $item->height,$resLoraCate->code ,$item->num);
                if(empty($res['images'])){
                    $data = [
                        'status' => TextToImageEnum::STATUS_IMAGE_ERR,
                    ];
                    $dep->edit($item->id,$data);
                    $this->log('image error',['id'=>$item->id]);
                    return true;
                }
                $imageArr = $res['images'];
                if(count($imageArr) > 1){
                    array_shift($imageArr);
                }
                $imagePathArr = [];
                foreach ($imageArr as $image){
                    $uuid = UidService::textToImageUid();
                    $outputPath = "{$resSystem->text_to_image_dir}\\$uuid.png";
                    base64ToImage($image,$outputPath);
                    $imagePathArr[] = "{$uuid}.png";
                }
                $data = [
                    'status' => TextToImageEnum::STATUS_IMAGE_SUCCESS,
                    'image_arr' => json_encode($imagePathArr),
                    'image_ok_at' => date( 'Y-m-d H:i:s')
                ];
                $dep->edit($item->id,$data);
                $this->log('image success',['id'=>$item->id]);
            }catch (\Exception $e){
                $data = [
                    'status' => TextToImageEnum::STATUS_IMAGE_ERR,
                ];
                $dep->edit($item->id,$data);
                $this->log('image error',['id'=>$item->id,'error'=>$e->getMessage()]);
                return true;
            }
        });
    }

    private function log($msg, $context = [])
    {
        $this->info("text to image log: {$msg} " . json_encode($context,JSON_UNESCAPED_UNICODE));
        log_daily("text-to-image")->info($msg, $context);
    }
}
